package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.exception.DaoException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.util.JDBCTemplate;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.systemSettings.dao.MenuDao;
import net.wanho.systemSettings.po.Menu;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class MenuDaoImpl implements MenuDao {
    private JDBCTemplate jdbcTemplate = BeanFactory.getBean(JDBCTemplate.class) ;
    @Override
    public List<Menu> selectMenuByPositionIdAndParentMenuId(Integer positionId, Integer parentMenuId) throws DaoException {
        final String SELECT_BASE_MENU_BY_POSITION_ID_AND_PARENT_MENU_ID_SQL = "SELECT\n" +
                "  MENU_ID           MENUID,\n" +
                "  MENU_NAME         MENUNAME,\n" +
                "  MENU_URL          MENUURL,\n" +
                "  PICTURES          PICTURES,\n" +
                "  STATUS            STATUS,\n" +
                "  CREATE_TIME       CREATETIME,\n" +
                "  UPDATE_TIME       UPDATETIME,\n" +
                "  PARENT_MENU_ID    PARENTMENUID\n" +
                "FROM menu\n" +
                "WHERE MENU_ID IN (SELECT\n" +
                "                   MENU_ID\n" +
                "                 FROM position_menu_relations\n" +
                "                 WHERE POSITION_ID = ?)\n" +
                "     AND\n" +
                "       STATUS = '0'" ;
        try {
            Object[] params ={positionId};
            StringBuffer sb = new StringBuffer(SELECT_BASE_MENU_BY_POSITION_ID_AND_PARENT_MENU_ID_SQL);
            if (parentMenuId==null){
                sb.append(" and PARENT_MENU_ID IS NULL") ;
            }else{
                sb.append(" and PARENT_MENU_ID = ? ") ;
                params = ArrayUtils.add(params,parentMenuId) ;
            }
            return jdbcTemplate.queryForList(sb.toString(), Menu.class,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DaoException(e.getMessage()) ;
        }
    }
}
